package models

import (
	fmt "fmt"
	time "time"

	git_querycap_com_tools_datatypes "git.querycap.com/tools/datatypes"
	github_com_go_courier_sqlx_v2 "github.com/go-courier/sqlx/v2"
	github_com_go_courier_sqlx_v2_builder "github.com/go-courier/sqlx/v2/builder"
	github_com_go_courier_sqlx_v2_datatypes "github.com/go-courier/sqlx/v2/datatypes"
)

func (DataSyncLog) PrimaryKey() []string {
	return []string{
		"ID",
	}
}

func (DataSyncLog) UniqueIndexUkDataSyncLogID() string {
	return "uk_data_sync_log_id"
}

func (DataSyncLog) UniqueIndexes() github_com_go_courier_sqlx_v2_builder.Indexes {
	return github_com_go_courier_sqlx_v2_builder.Indexes{
		"uk_data_sync_log_id": []string{
			"DataSyncLogID",
			"DeletedAt",
		},
	}
}

func (DataSyncLog) Comments() map[string]string {
	return map[string]string{
		"CreatedAt":     "创建时间",
		"DataSyncLogID": "同步日志ID",
		"DeletedAt":     "删除时间",
		"ID":            "自增ID",
		"PlatformID":    "平台ID",
		"SyncFailure":   "同步设备失败数量",
		"SyncLog":       "同步日志",
		"SyncStatus":    "同步状态",
		"SyncSuccess":   "同步设备成功数量",
		"SyncTotal":     "同步设备总数",
		"UpdatedAt":     "更新时间",
	}
}

var DataSyncLogTable *github_com_go_courier_sqlx_v2_builder.Table

func init() {
	DataSyncLogTable = DBVideoHub.Register(&DataSyncLog{})
}

type DataSyncLogIterator struct {
}

func (DataSyncLogIterator) New() interface{} {
	return &DataSyncLog{}
}

func (DataSyncLogIterator) Resolve(v interface{}) *DataSyncLog {
	return v.(*DataSyncLog)
}

func (DataSyncLog) TableName() string {
	return "t_data_sync_log"
}

func (DataSyncLog) TableDescription() []string {
	return []string{
		"设备同步日志表",
	}
}

func (DataSyncLog) ColDescriptions() map[string][]string {
	return map[string][]string{
		"CreatedAt": []string{
			"创建时间",
		},
		"DataSyncLogID": []string{
			"同步日志ID",
		},
		"DeletedAt": []string{
			"删除时间",
		},
		"ID": []string{
			"自增ID",
		},
		"PlatformID": []string{
			"平台ID",
		},
		"SyncFailure": []string{
			"同步设备失败数量",
		},
		"SyncLog": []string{
			"同步日志",
		},
		"SyncStatus": []string{
			"同步状态",
		},
		"SyncSuccess": []string{
			"同步设备成功数量",
		},
		"SyncTotal": []string{
			"同步设备总数",
		},
		"UpdatedAt": []string{
			"更新时间",
		},
	}
}

func (DataSyncLog) FieldKeyID() string {
	return "ID"
}

func (m *DataSyncLog) FieldID() *github_com_go_courier_sqlx_v2_builder.Column {
	return DataSyncLogTable.F(m.FieldKeyID())
}

func (DataSyncLog) FieldKeyDataSyncLogID() string {
	return "DataSyncLogID"
}

func (m *DataSyncLog) FieldDataSyncLogID() *github_com_go_courier_sqlx_v2_builder.Column {
	return DataSyncLogTable.F(m.FieldKeyDataSyncLogID())
}

func (DataSyncLog) FieldKeyPlatformID() string {
	return "PlatformID"
}

func (m *DataSyncLog) FieldPlatformID() *github_com_go_courier_sqlx_v2_builder.Column {
	return DataSyncLogTable.F(m.FieldKeyPlatformID())
}

func (DataSyncLog) FieldKeySyncTotal() string {
	return "SyncTotal"
}

func (m *DataSyncLog) FieldSyncTotal() *github_com_go_courier_sqlx_v2_builder.Column {
	return DataSyncLogTable.F(m.FieldKeySyncTotal())
}

func (DataSyncLog) FieldKeySyncSuccess() string {
	return "SyncSuccess"
}

func (m *DataSyncLog) FieldSyncSuccess() *github_com_go_courier_sqlx_v2_builder.Column {
	return DataSyncLogTable.F(m.FieldKeySyncSuccess())
}

func (DataSyncLog) FieldKeySyncFailure() string {
	return "SyncFailure"
}

func (m *DataSyncLog) FieldSyncFailure() *github_com_go_courier_sqlx_v2_builder.Column {
	return DataSyncLogTable.F(m.FieldKeySyncFailure())
}

func (DataSyncLog) FieldKeySyncStatus() string {
	return "SyncStatus"
}

func (m *DataSyncLog) FieldSyncStatus() *github_com_go_courier_sqlx_v2_builder.Column {
	return DataSyncLogTable.F(m.FieldKeySyncStatus())
}

func (DataSyncLog) FieldKeySyncLog() string {
	return "SyncLog"
}

func (m *DataSyncLog) FieldSyncLog() *github_com_go_courier_sqlx_v2_builder.Column {
	return DataSyncLogTable.F(m.FieldKeySyncLog())
}

func (DataSyncLog) FieldKeyCreatedAt() string {
	return "CreatedAt"
}

func (m *DataSyncLog) FieldCreatedAt() *github_com_go_courier_sqlx_v2_builder.Column {
	return DataSyncLogTable.F(m.FieldKeyCreatedAt())
}

func (DataSyncLog) FieldKeyUpdatedAt() string {
	return "UpdatedAt"
}

func (m *DataSyncLog) FieldUpdatedAt() *github_com_go_courier_sqlx_v2_builder.Column {
	return DataSyncLogTable.F(m.FieldKeyUpdatedAt())
}

func (DataSyncLog) FieldKeyDeletedAt() string {
	return "DeletedAt"
}

func (m *DataSyncLog) FieldDeletedAt() *github_com_go_courier_sqlx_v2_builder.Column {
	return DataSyncLogTable.F(m.FieldKeyDeletedAt())
}

func (DataSyncLog) ColRelations() map[string][]string {
	return map[string][]string{
		"DataSyncLogID": []string{
			"DataSyncLog",
			"DataSyncLogID",
		},
		"PlatformID": []string{
			"Platform",
			"PlatformID",
		},
	}
}

func (m *DataSyncLog) IndexFieldNames() []string {
	return []string{
		"DataSyncLogID",
		"ID",
	}
}

func (m *DataSyncLog) ConditionByStruct(db github_com_go_courier_sqlx_v2.DBExecutor) github_com_go_courier_sqlx_v2_builder.SqlCondition {
	table := db.T(m)
	fieldValues := github_com_go_courier_sqlx_v2_builder.FieldValuesFromStructByNonZero(m)

	conditions := make([]github_com_go_courier_sqlx_v2_builder.SqlCondition, 0)

	for _, fieldName := range m.IndexFieldNames() {
		if v, exists := fieldValues[fieldName]; exists {
			conditions = append(conditions, table.F(fieldName).Eq(v))
			delete(fieldValues, fieldName)
		}
	}

	if len(conditions) == 0 {
		panic(fmt.Errorf("at least one of field for indexes has value"))
	}

	for fieldName, v := range fieldValues {
		conditions = append(conditions, table.F(fieldName).Eq(v))
	}

	condition := github_com_go_courier_sqlx_v2_builder.And(conditions...)

	condition = github_com_go_courier_sqlx_v2_builder.And(condition, table.F("DeletedAt").Eq(0))
	return condition
}

func (m *DataSyncLog) Create(db github_com_go_courier_sqlx_v2.DBExecutor) error {

	if m.CreatedAt.IsZero() {
		m.CreatedAt = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now())
	}

	if m.UpdatedAt.IsZero() {
		m.UpdatedAt = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now())
	}

	_, err := db.ExecExpr(github_com_go_courier_sqlx_v2.InsertToDB(db, m, nil))
	return err

}

func (m *DataSyncLog) CreateOnDuplicateWithUpdateFields(db github_com_go_courier_sqlx_v2.DBExecutor, updateFields []string) error {

	if len(updateFields) == 0 {
		panic(fmt.Errorf("must have update fields"))
	}

	if m.CreatedAt.IsZero() {
		m.CreatedAt = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now())
	}

	if m.UpdatedAt.IsZero() {
		m.UpdatedAt = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now())
	}

	fieldValues := github_com_go_courier_sqlx_v2_builder.FieldValuesFromStructByNonZero(m, updateFields...)

	delete(fieldValues, "ID")

	table := db.T(m)

	cols, vals := table.ColumnsAndValuesByFieldValues(fieldValues)

	fields := make(map[string]bool, len(updateFields))
	for _, field := range updateFields {
		fields[field] = true
	}

	for _, fieldNames := range m.UniqueIndexes() {
		for _, field := range fieldNames {
			delete(fields, field)
		}
	}

	if len(fields) == 0 {
		panic(fmt.Errorf("no fields for updates"))
	}

	for field := range fieldValues {
		if !fields[field] {
			delete(fieldValues, field)
		}
	}

	additions := github_com_go_courier_sqlx_v2_builder.Additions{}

	switch db.Dialect().DriverName() {
	case "mysql":
		additions = append(additions, github_com_go_courier_sqlx_v2_builder.OnDuplicateKeyUpdate(table.AssignmentsByFieldValues(fieldValues)...))
	case "postgres":
		indexes := m.UniqueIndexes()
		fields := make([]string, 0)
		for _, fs := range indexes {
			fields = append(fields, fs...)
		}
		indexFields, _ := db.T(m).Fields(fields...)

		additions = append(additions,
			github_com_go_courier_sqlx_v2_builder.OnConflict(indexFields).
				DoUpdateSet(table.AssignmentsByFieldValues(fieldValues)...))
	}

	additions = append(additions, github_com_go_courier_sqlx_v2_builder.Comment("User.CreateOnDuplicateWithUpdateFields"))

	expr := github_com_go_courier_sqlx_v2_builder.Insert().Into(table, additions...).Values(cols, vals...)

	_, err := db.ExecExpr(expr)
	return err

}

func (m *DataSyncLog) DeleteByStruct(db github_com_go_courier_sqlx_v2.DBExecutor) error {

	_, err := db.ExecExpr(
		github_com_go_courier_sqlx_v2_builder.Delete().
			From(
				db.T(m),
				github_com_go_courier_sqlx_v2_builder.Where(m.ConditionByStruct(db)),
				github_com_go_courier_sqlx_v2_builder.Comment("DataSyncLog.DeleteByStruct"),
			),
	)

	return err
}

func (m *DataSyncLog) FetchByID(db github_com_go_courier_sqlx_v2.DBExecutor) error {

	table := db.T(m)

	err := db.QueryExprAndScan(
		github_com_go_courier_sqlx_v2_builder.Select(nil).
			From(
				db.T(m),
				github_com_go_courier_sqlx_v2_builder.Where(github_com_go_courier_sqlx_v2_builder.And(
					table.F("ID").Eq(m.ID),
					table.F("DeletedAt").Eq(m.DeletedAt),
				)),
				github_com_go_courier_sqlx_v2_builder.Comment("DataSyncLog.FetchByID"),
			),
		m,
	)

	return err
}

func (m *DataSyncLog) UpdateByIDWithMap(db github_com_go_courier_sqlx_v2.DBExecutor, fieldValues github_com_go_courier_sqlx_v2_builder.FieldValues) error {

	if _, ok := fieldValues["UpdatedAt"]; !ok {
		fieldValues["UpdatedAt"] = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now())
	}

	table := db.T(m)

	result, err := db.ExecExpr(
		github_com_go_courier_sqlx_v2_builder.Update(db.T(m)).
			Where(
				github_com_go_courier_sqlx_v2_builder.And(
					table.F("ID").Eq(m.ID),
					table.F("DeletedAt").Eq(m.DeletedAt),
				),
				github_com_go_courier_sqlx_v2_builder.Comment("DataSyncLog.UpdateByIDWithMap"),
			).
			Set(table.AssignmentsByFieldValues(fieldValues)...),
	)

	if err != nil {
		return err
	}

	rowsAffected, _ := result.RowsAffected()
	if rowsAffected == 0 {
		return m.FetchByID(db)
	}

	return nil

}

func (m *DataSyncLog) UpdateByIDWithStruct(db github_com_go_courier_sqlx_v2.DBExecutor, zeroFields ...string) error {

	fieldValues := github_com_go_courier_sqlx_v2_builder.FieldValuesFromStructByNonZero(m, zeroFields...)
	return m.UpdateByIDWithMap(db, fieldValues)

}

func (m *DataSyncLog) FetchByIDForUpdate(db github_com_go_courier_sqlx_v2.DBExecutor) error {

	table := db.T(m)

	err := db.QueryExprAndScan(
		github_com_go_courier_sqlx_v2_builder.Select(nil).
			From(
				db.T(m),
				github_com_go_courier_sqlx_v2_builder.Where(github_com_go_courier_sqlx_v2_builder.And(
					table.F("ID").Eq(m.ID),
					table.F("DeletedAt").Eq(m.DeletedAt),
				)),
				github_com_go_courier_sqlx_v2_builder.ForUpdate(),
				github_com_go_courier_sqlx_v2_builder.Comment("DataSyncLog.FetchByIDForUpdate"),
			),
		m,
	)

	return err
}

func (m *DataSyncLog) DeleteByID(db github_com_go_courier_sqlx_v2.DBExecutor) error {

	table := db.T(m)

	_, err := db.ExecExpr(
		github_com_go_courier_sqlx_v2_builder.Delete().
			From(db.T(m),
				github_com_go_courier_sqlx_v2_builder.Where(github_com_go_courier_sqlx_v2_builder.And(
					table.F("ID").Eq(m.ID),
					table.F("DeletedAt").Eq(m.DeletedAt),
				)),
				github_com_go_courier_sqlx_v2_builder.Comment("DataSyncLog.DeleteByID"),
			))

	return err
}

func (m *DataSyncLog) SoftDeleteByID(db github_com_go_courier_sqlx_v2.DBExecutor) error {

	table := db.T(m)

	fieldValues := github_com_go_courier_sqlx_v2_builder.FieldValues{}
	if _, ok := fieldValues["DeletedAt"]; !ok {
		fieldValues["DeletedAt"] = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now())
	}

	if _, ok := fieldValues["UpdatedAt"]; !ok {
		fieldValues["UpdatedAt"] = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now())
	}

	_, err := db.ExecExpr(
		github_com_go_courier_sqlx_v2_builder.Update(db.T(m)).
			Where(
				github_com_go_courier_sqlx_v2_builder.And(
					table.F("ID").Eq(m.ID),
					table.F("DeletedAt").Eq(m.DeletedAt),
				),
				github_com_go_courier_sqlx_v2_builder.Comment("DataSyncLog.SoftDeleteByID"),
			).
			Set(table.AssignmentsByFieldValues(fieldValues)...),
	)

	return err

}

func (m *DataSyncLog) FetchByDataSyncLogID(db github_com_go_courier_sqlx_v2.DBExecutor) error {

	table := db.T(m)

	err := db.QueryExprAndScan(
		github_com_go_courier_sqlx_v2_builder.Select(nil).
			From(
				db.T(m),
				github_com_go_courier_sqlx_v2_builder.Where(github_com_go_courier_sqlx_v2_builder.And(
					table.F("DataSyncLogID").Eq(m.DataSyncLogID),
					table.F("DeletedAt").Eq(m.DeletedAt),
				)),
				github_com_go_courier_sqlx_v2_builder.Comment("DataSyncLog.FetchByDataSyncLogID"),
			),
		m,
	)

	return err
}

func (m *DataSyncLog) UpdateByDataSyncLogIDWithMap(db github_com_go_courier_sqlx_v2.DBExecutor, fieldValues github_com_go_courier_sqlx_v2_builder.FieldValues) error {

	if _, ok := fieldValues["UpdatedAt"]; !ok {
		fieldValues["UpdatedAt"] = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now())
	}

	table := db.T(m)

	result, err := db.ExecExpr(
		github_com_go_courier_sqlx_v2_builder.Update(db.T(m)).
			Where(
				github_com_go_courier_sqlx_v2_builder.And(
					table.F("DataSyncLogID").Eq(m.DataSyncLogID),
					table.F("DeletedAt").Eq(m.DeletedAt),
				),
				github_com_go_courier_sqlx_v2_builder.Comment("DataSyncLog.UpdateByDataSyncLogIDWithMap"),
			).
			Set(table.AssignmentsByFieldValues(fieldValues)...),
	)

	if err != nil {
		return err
	}

	rowsAffected, _ := result.RowsAffected()
	if rowsAffected == 0 {
		return m.FetchByDataSyncLogID(db)
	}

	return nil

}

func (m *DataSyncLog) UpdateByDataSyncLogIDWithStruct(db github_com_go_courier_sqlx_v2.DBExecutor, zeroFields ...string) error {

	fieldValues := github_com_go_courier_sqlx_v2_builder.FieldValuesFromStructByNonZero(m, zeroFields...)
	return m.UpdateByDataSyncLogIDWithMap(db, fieldValues)

}

func (m *DataSyncLog) FetchByDataSyncLogIDForUpdate(db github_com_go_courier_sqlx_v2.DBExecutor) error {

	table := db.T(m)

	err := db.QueryExprAndScan(
		github_com_go_courier_sqlx_v2_builder.Select(nil).
			From(
				db.T(m),
				github_com_go_courier_sqlx_v2_builder.Where(github_com_go_courier_sqlx_v2_builder.And(
					table.F("DataSyncLogID").Eq(m.DataSyncLogID),
					table.F("DeletedAt").Eq(m.DeletedAt),
				)),
				github_com_go_courier_sqlx_v2_builder.ForUpdate(),
				github_com_go_courier_sqlx_v2_builder.Comment("DataSyncLog.FetchByDataSyncLogIDForUpdate"),
			),
		m,
	)

	return err
}

func (m *DataSyncLog) DeleteByDataSyncLogID(db github_com_go_courier_sqlx_v2.DBExecutor) error {

	table := db.T(m)

	_, err := db.ExecExpr(
		github_com_go_courier_sqlx_v2_builder.Delete().
			From(db.T(m),
				github_com_go_courier_sqlx_v2_builder.Where(github_com_go_courier_sqlx_v2_builder.And(
					table.F("DataSyncLogID").Eq(m.DataSyncLogID),
					table.F("DeletedAt").Eq(m.DeletedAt),
				)),
				github_com_go_courier_sqlx_v2_builder.Comment("DataSyncLog.DeleteByDataSyncLogID"),
			))

	return err
}

func (m *DataSyncLog) SoftDeleteByDataSyncLogID(db github_com_go_courier_sqlx_v2.DBExecutor) error {

	table := db.T(m)

	fieldValues := github_com_go_courier_sqlx_v2_builder.FieldValues{}
	if _, ok := fieldValues["DeletedAt"]; !ok {
		fieldValues["DeletedAt"] = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now())
	}

	if _, ok := fieldValues["UpdatedAt"]; !ok {
		fieldValues["UpdatedAt"] = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now())
	}

	_, err := db.ExecExpr(
		github_com_go_courier_sqlx_v2_builder.Update(db.T(m)).
			Where(
				github_com_go_courier_sqlx_v2_builder.And(
					table.F("DataSyncLogID").Eq(m.DataSyncLogID),
					table.F("DeletedAt").Eq(m.DeletedAt),
				),
				github_com_go_courier_sqlx_v2_builder.Comment("DataSyncLog.SoftDeleteByDataSyncLogID"),
			).
			Set(table.AssignmentsByFieldValues(fieldValues)...),
	)

	return err

}

func (m *DataSyncLog) List(db github_com_go_courier_sqlx_v2.DBExecutor, condition github_com_go_courier_sqlx_v2_builder.SqlCondition, additions ...github_com_go_courier_sqlx_v2_builder.Addition) ([]DataSyncLog, error) {

	list := make([]DataSyncLog, 0)

	table := db.T(m)
	_ = table

	condition = github_com_go_courier_sqlx_v2_builder.And(condition, table.F("DeletedAt").Eq(0))

	finalAdditions := []github_com_go_courier_sqlx_v2_builder.Addition{
		github_com_go_courier_sqlx_v2_builder.Where(condition),
		github_com_go_courier_sqlx_v2_builder.Comment("DataSyncLog.List"),
	}

	if len(additions) > 0 {
		finalAdditions = append(finalAdditions, additions...)
	}

	err := db.QueryExprAndScan(
		github_com_go_courier_sqlx_v2_builder.Select(nil).
			From(db.T(m), finalAdditions...),
		&list,
	)

	return list, err

}

func (m *DataSyncLog) Count(db github_com_go_courier_sqlx_v2.DBExecutor, condition github_com_go_courier_sqlx_v2_builder.SqlCondition, additions ...github_com_go_courier_sqlx_v2_builder.Addition) (int, error) {

	count := -1

	table := db.T(m)
	_ = table

	condition = github_com_go_courier_sqlx_v2_builder.And(condition, table.F("DeletedAt").Eq(0))

	finalAdditions := []github_com_go_courier_sqlx_v2_builder.Addition{
		github_com_go_courier_sqlx_v2_builder.Where(condition),
		github_com_go_courier_sqlx_v2_builder.Comment("DataSyncLog.Count"),
	}

	if len(additions) > 0 {
		finalAdditions = append(finalAdditions, additions...)
	}

	err := db.QueryExprAndScan(
		github_com_go_courier_sqlx_v2_builder.Select(
			github_com_go_courier_sqlx_v2_builder.Count(),
		).
			From(db.T(m), finalAdditions...),
		&count,
	)

	return count, err

}

func (m *DataSyncLog) BatchFetchByDataSyncLogIDList(db github_com_go_courier_sqlx_v2.DBExecutor, values []git_querycap_com_tools_datatypes.SFID) ([]DataSyncLog, error) {

	if len(values) == 0 {
		return nil, nil
	}

	table := db.T(m)

	condition := table.F("DataSyncLogID").In(values)

	return m.List(db, condition)

}

func (m *DataSyncLog) BatchFetchByIDList(db github_com_go_courier_sqlx_v2.DBExecutor, values []uint64) ([]DataSyncLog, error) {

	if len(values) == 0 {
		return nil, nil
	}

	table := db.T(m)

	condition := table.F("ID").In(values)

	return m.List(db, condition)

}
